package io.itit.ecp.admin.provide.renderer;

import com.alibaba.fastjson.serializer.ValueFilter;
import io.itit.ecp.admin.provide.filter.IDeviceFilter;
import io.itit.ecp.admin.server.entity.*;
import io.itit.ecp.admin.server.entity.extend.DeviceExtEntity;
import io.itit.ecp.admin.server.service.*;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.grass.estelle.starter.parse.AbstractParseRenderer;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：设备管理导入解析器
 * @Date：Create by 2023-11-30 18:51:39
 */
@Scope("prototype")
@Component(DeviceRenderer.ENTITY_NAME)
public class DeviceRenderer extends AbstractParseRenderer<DeviceExtEntity> {

    public static final String ENTITY_NAME = "deviceRenderer";

    @Override
    public int getHeadRowIndex() {
        return 1;
    }

    @Resource
    private IDeviceService deviceService;

    @Resource
    private ISceneProjectService sceneProjectService;

    @Resource
    private ISceneEdificeService sceneEdificeService;

    @Resource
    private ISceneFloorService sceneFloorService;

    @Resource
    private ISceneRoomService sceneRoomService;

    @Resource
    private IProductTypeService productTypeService;

    private Set<String> dataSet;

    private Set<String> keySet = new HashSet<>();

    @Override
    public String entityName() {
        return ENTITY_NAME;
    }

    @Override
    public Class<DeviceExtEntity> getClazz() {
        return DeviceExtEntity.class;
    }

    @Override
    public ValueFilter getValueFilter() {
        return new IDeviceFilter.DeviceUploadFilter();
    }


    @Override
    public void processBefore() {

        dataSet = deviceService.lambdaQuery().list()
                .stream().map(t -> t.getDeviceCode() +
                        t.getDeviceName())
                .collect(Collectors.toSet());

    }
    private String key(String DeviceCode) {
        return DeviceCode;
    }
    @Override
    public void verify(DeviceExtEntity entity) {
        if (StringUtils.isNotEmpty(entity.getProductTypeId())){
            ProductTypeEntity productType = productTypeService.lambdaQuery()
                    .eq(ProductTypeEntity::getProductTypeName, entity.getProductTypeId())
                    .one();
            if (productType == null){
                addErrorMsg("产品类型不存在");
            }else{
                entity.setProductTypeId(productType.getId());
            }
        }
        if (StringUtils.isNotEmpty(entity.getSceneFloorId())){
            SceneProjectEntity project = sceneProjectService.lambdaQuery()
                    .eq(SceneProjectEntity::getProjectName, entity.getSceneProjectId())
                    .one();
            if (project == null){
                addErrorMsg("项目名称不存在");
                return;
            }else{
                entity.setSceneProjectId(project.getId());
            }
        }
        if (StringUtils.isNotEmpty(entity.getSceneEdificeId())){
            SceneEdificeEntity edifice = sceneEdificeService.lambdaQuery()
                    .eq(SceneEdificeEntity::getSceneProjectId, entity.getSceneProjectId())
                    .eq(SceneEdificeEntity::getEdificeName, entity.getSceneEdificeId())
                    .one();
            if (edifice == null){
                addErrorMsg("楼栋名称不存在");
                return;
            }else{
                entity.setSceneEdificeId(edifice.getId());
            }
        }
        if (StringUtils.isNotEmpty(entity.getSceneFloorId())){
            SceneFloorEntity floor = sceneFloorService.lambdaQuery()
                    .eq(SceneFloorEntity::getSceneEdificeId, entity.getSceneEdificeId())
                    .eq(SceneFloorEntity::getFloorName, entity.getSceneFloorId())
                    .one();
            if (floor == null){
                addErrorMsg("楼层名称不存在");
            }else{
                entity.setSceneFloorId(floor.getId());
            }
        }
        if (StringUtils.isNotEmpty(entity.getSceneRoomId())){
            SceneRoomEntity room = sceneRoomService.lambdaQuery()
                    .eq(SceneRoomEntity::getSceneFloorId, entity.getSceneFloorId())
                    .eq(SceneRoomEntity::getRoomName, entity.getSceneRoomId())
                    .one();
            if (room == null){
                addErrorMsg("楼层名称不存在");
            }else{
                entity.setSceneRoomId(room.getId());
            }
        }
        List<DeviceEntity> code = deviceService.lambdaQuery()
                .eq(DeviceEntity::getSceneProjectId, entity.getSceneProjectId())
                .eq(DeviceEntity::getDeviceName,entity.getDeviceName())
                .list();
        String key = key(entity.getDeviceCode());

        String message = "设备编码【" + entity.getDeviceCode()
                + "】设备名称【" + entity.getDeviceName();

        addErrorMsg(code.size() > 0, message + "】, 数据已存在！");
        addErrorMsg(keySet.contains(key), message + "】, 在导入文件中出现重复！");
        keySet.add(key);
    }

    @Override
    public int processAfter(List<DeviceExtEntity> resultList) {
        if (CollectionUtils.isEmpty(resultList)) {
            return 0;
        }
        List<DeviceEntity> entities = resultList.stream()
                .map(ser-> ConvertUtils.copy(ser, DeviceEntity.class))
                .collect(Collectors.toList());
        deviceService.saveDeviceBatch(entities);
        return resultList.size();
    }
}
